class Block < ActiveRecord::Base
  belongs_to :account
  validates_presence_of :service_id, :account_id, :summa
  
  def self.change(id, amount)
    @block = find(id)
    if(@block.summa == amount)
      @block.modify_time = Time.now.to_date
      return @block.save!
    end
  end
  
  def self.commit_block(id)
    return nil if !block = find(id)
    return nil if block.is_open == 0
    transaction do
      service_account = Account.find(Service.find(block.service_id).account_id)
      transaction_id = Account.move_funds(block.account_id, service_account.id, block.summa)
      block.transaction_id = transaction_id
      block.is_open = 0
      block.save!
      transaction_id
    end
  end
  
end
